﻿using EmoneyWeiXinCommon;
using EmoneyWeiXinEntity;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using tenpayApp;

namespace ActivtyUI.App_Code
{
    public class WeiXinJsSign
    {
        static MemcachedTools1 mmtols = new MemcachedTools1();
        public static string getNoncestr()
        {
            Random random = new Random();
            return MD5Util.GetMD5(random.Next(1000).ToString(), "UTF-8");
        }

        public static string getTimestamp()
        {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return Convert.ToInt64(ts.TotalSeconds).ToString();
        }



        //public static string getWxTicket(string appid, string appsercret)
        //{
        //    string url = Config.WeiXin_ApiUrl + "ticket/getticket";
        //    string funcRet = "";
        //    try
        //    {
        //        MemcachedTools1 mmtools = new MemcachedTools1();

        //        string ticket_key = StringTools.ChangeRealKey("jsapi_ticket", "lzc");

        //        var jsapi_ticket = mmtols.Get(ticket_key);
        //        if (jsapi_ticket != null)
        //        {
        //            MyTrace.get().add("getWxTicket----从缓存中取出：jsapi_ticket" + jsapi_ticket);
        //            return jsapi_ticket.ToString();
        //        }
        //        string token = TokenHelper.GetToken(appid, appsercret);
        //        if (string.IsNullOrEmpty(token))
        //        {
        //            MyTrace.get().add("getWxTicket----获取的token为空 appid:" + appid + " appSercret:" + appsercret);
        //            return funcRet;
        //        }
        //        Dictionary<string, string> urlParams = new Dictionary<string, string>();
        //        urlParams.Add("access_token", token);
        //        urlParams.Add("type", "jsapi");
        //        string ret = NetHelper.LoadData(url, urlParams);

        //        if (ret.IndexOf("{\"errcode\":40001") != -1)
        //        {
        //            MyTrace.get().add("getWxTicket----token失效");
        //            TokenHelper.ClearToken();

        //            urlParams = new Dictionary<string, string>();
        //            urlParams.Add("access_token", TokenHelper.GetToken(appid, appsercret));
        //            ret = NetHelper.LoadData(url, urlParams);
        //        }
        //        MyTrace.get().add("getWxTicket----ret：" + ret);
        //        JObject jObj = JObject.Parse(ret);
        //        if (jObj != null)
        //        {
        //            JToken jToken;
        //            if (jObj.TryGetValue("ticket", out jToken))
        //            {
        //                funcRet = jObj["ticket"].ToString().Replace("\"", "");
        //                MyTrace.get().add("getWxTicket----从响应数据中取出：funcRet" + funcRet);
        //                //需要存储在缓存里
        //                if (!string.IsNullOrEmpty(funcRet))
        //                {
        //                    //CacheManager.RedisCache.Set<String>(ticket_key, funcRet, 60);
        //                    mmtols.Add(ticket_key, funcRet, 0, 60, 0);
        //                }
        //            }
        //        }

        //    }
        //    catch (Exception ex)
        //    {
        //        MyTrace.get().add("getWxTicket;发生错误" + ex.Message);
        //    }
        //    return funcRet;
        //}


        public static string getWxSign(string noncestr, string timestamp, weixinUserEntity wxInfo, string url)
        {
            //TokenHelper.GetTicket()
            var ticket =TokenHelper.GetTicket(wxInfo.AppId, wxInfo.AppSecret);
            //var url = requestContext.HttpContext.Request.RawUrl;
            var s = "jsapi_ticket={0}&noncestr={1}&timestamp={2}&url={3}";
            s = string.Format(s, ticket, noncestr, timestamp, url);
            s = SHA1Util.getSha1(s).ToString().ToLower();
            MyTrace.get().debug("ticket:" + ticket + "\n noncestr:" + noncestr + "\n timestamp" + timestamp + "\n url:" + url + "\n resultSign" + s);
            return s;

        }

    }
}